package com.haoma.task;

import cn.hutool.core.date.DateUtil;
import com.haoma.entity.Request;
import com.haoma.entity.Visitor;
import com.haoma.mapper.RequestMapper;
import com.haoma.mapper.VisitorMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * Description: 每天12点统计今天的访客量
 */
@Component
public class VisitorTask {

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private VisitorMapper visitorMapper;

    @Autowired
    private RequestMapper requestMapper;

    // 定时任务方法，每天23:50执行一次将当前访问数据同步到数据库中
    @Scheduled(cron = "0 58 23 * * ?")
    // @Scheduled(cron = "0/20 * * * * ?")
    public void saveVisitor() {
        // 获取当前日期
        Date date = new Date();
        String jsonFormat = DateUtil.format(date, "yyyy-MM-dd") + "visitor";
        // 查询redis是否有缓存
        Object userCount = redisTemplate.opsForValue().get(jsonFormat);
        if (userCount == null) {
            return;
        }
        Integer count = Integer.parseInt((String) userCount);
        // 存入数据库中
        Visitor visitor = new Visitor();
        visitor.setCount(count);
        visitor.setTime(jsonFormat.substring(0, 10));
        visitorMapper.insert(visitor);
        // 清除当天缓存
        redisTemplate.delete(jsonFormat);
    }

    // 定时任务方法，每天23:50执行一次将当前接口访问数据同步到数据库中
    // @Scheduled(cron = "0/30 * * * * ?")
    @Scheduled(cron = "0 58 23 * * ?")
    public void saveRequest() {
        // 获取当前日期
        Date date = new Date();
        String jsonFormat = DateUtil.format(date, "yyyy-MM-dd") + "request";
        // 查询redis是否有缓存
        Map<Object, Object> entries = redisTemplate.opsForHash().entries(jsonFormat);
        if (entries.isEmpty()) {
            return;
        }
        Set<Request> requestList = new HashSet<>();

        // 获取到当前时间戳
        long timeId = System.currentTimeMillis();
        int i = 0;
        // 存入缓存
        for (Map.Entry<Object, Object> objectObjectEntry : entries.entrySet()) {
            Request request = new Request();
            request.setId(timeId + i);
            request.setPath((String) objectObjectEntry.getKey());
            request.setCount((Integer) objectObjectEntry.getValue());
            requestList.add(request);
            i++;
        }
        Integer count = 0;
        // 统计数量
        for (Request request : requestList) {
            Integer count1 = request.getCount();
            count = count1 + count;
        }
        // 判断
        Object o = redisTemplate.opsForValue().get("request-count");
        if (o == null) {
            // 存入redis中
            redisTemplate.opsForValue().set("request-count", count);
        } else {
            Integer countNumber = Integer.parseInt((String) o);
            redisTemplate.opsForValue().set("request-count", countNumber + count);
        }
        for (Request request : requestList) {
            requestMapper.insert(request);
        }
        // 清空数据库数据
        requestMapper.deleteByPath(requestList);
        // 清空当前hash
        redisTemplate.delete(jsonFormat);
    }

}
